﻿//var element=document.createElement('script'); 
//element.setAttribute('src', chrome.extension.getURL("dragsave.js")); 
//document.body.appendChild(element);

var body = document.getElementsByTagName("body")[0];
var last_frame = null;
var last_div = null;
var g_bDisable = false;
var div_num = 0;

//检查翻译窗口是否存在，如果存在则关闭它
function checkCloseWindow() {
  if (last_div != null) {
    if(last_div)
    {
      body.removeChild(last_div);
    }
    last_div = null;
    return true;
  }
  return false
}

function flashWindow() {
  if (last_div != null) {
    if(last_div)
    {
      //body.removeChild(last_div);
	  //last_div.style.border = "1px solid ";
	  last_div.style.backgroundColor = '#CCC';
	  //last_div.innerHTML="摘抄成功！";
    }
    //last_div = null;
    return true;
  }
  return false
}

// 说明：获取鼠标位置 
function mousePosition(ev){ 
    if(ev.pageX || ev.pageY){ 
        return {x:ev.pageX, y:ev.pageY}; 
    } 
    return { 
        x:ev.clientX + document.body.scrollLeft - document.body.clientLeft, 
        y:ev.clientY + document.body.scrollTop  - document.body.clientTop 
    }; 
} 

function dragOver (e) {
  if (start_x == -1 || start_y == -1) {
    // The dragover event is from external, keep original action.
    return true;
  }
  if (e.preventDefault) {
    e.preventDefault ();
  }
  return false;
}
var div_toolbar = null;
function dragStart(e) {
  checkCloseWindow();
  start_x = e.screenX;
  start_y = e.screenY;
  var posMouse = mousePosition(e);
  div_toolbar = document.createElement('div');
  //div_toolbar.innerHTML = "<span style='font-size:24.0pt;font-family:Webdings;color:green'>P</span>";
  div_toolbar.innerHTML = "<img src='" + chrome.extension.getURL("logo_32.png") + "' width=32/>"
  div_toolbar.style.left = (posMouse.x+20)  + 'px';
  div_toolbar.style.top = (posMouse.y-20) + 'px';
  div_toolbar.style.position = 'absolute';
  div_toolbar.style.width = 33 + "px";  
  div_toolbar.style.height = '33px';
  //div_toolbar.style.border = '1px solid ';
  //div_toolbar.style.backgroundColor = '#FFF';
  div_toolbar.style.zIndex = '65535';
  //div_toolbar.style.border = '1px solid ' + colors[optVal("color_type")][1];//optVal("links_color");
  //div_toolbar.style.backgroundColor = colors[optVal("color_type")][0];
  body.appendChild(div_toolbar);
  
  div_toolbar.addEventListener('drop', dragDrop);
  
  last_div = div_toolbar;
}

function dragDrop(e) {
  if (start_x == -1 || start_y == -1) {
    // The drop event is from external, keep original action.
    return true;
  }
  var x_dir = 1;
  if (e.preventDefault) {
    e.preventDefault ();
  }
  if (e.screenX < start_x) {
    x_dir = -1;
  }
  var y_dir = 1;
  if (e.screenY < start_y) {
    y_dir = -1;
  }
  start_x = -1;
  start_y = -1;
  var data = e.dataTransfer.getData('URL');
  if (!data) {
    data = e.dataTransfer.getData('Text');
  }
  if (data) {
    clipBody = data; 
    if(clipBody.length<36)
	  {
		return;
	  }
    //var clipTitle = clipBody.substring(0,36);
	var clipTitle = document.title;
	SaveNewWord(clipTitle,clipBody,window.location.href,0);
	//chrome.extension.connect().postMessage({
    //  message: 'tab', values: data, x_dir: x_dir, y_dir: y_dir});
	
	  if (e.preventDefault) {
		e.preventDefault ();
	  }
    //alert(data);
	return false;
  }
  return true;
}

function SaveNewWord(w,s,purl,pos)
{
  chrome.extension.sendRequest(
    {
        init: "new",
        title: w,
        body:s,
        wordurl: purl,
        wordpos: pos
        
    },
    function(response)
    {
      //alert("dream?");
	  //checkCloseWindow();
	  flashWindow();
      //alert(response);
    }
  );
}

start_x = -1;
start_y = -1;
document.addEventListener('mousedown', function (e){
checkCloseWindow();
},false);
document.addEventListener('dragstart', dragStart, false);
document.addEventListener('dragover', dragOver, false);
document.addEventListener('drop', function (e){
//checkCloseWindow();
},false);